/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.entityGenerator.base.elements;

import com.inspur.edp.caf.generator.baseInfo.ParameterInfo;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.method.ClassMethodGenerator;
import com.inspur.edp.cef.designtime.api.IGspCommonField;
import com.inspur.edp.cef.designtime.api.element.EnumIndexType;
import jdk.nashorn.internal.ir.annotations.Ignore;
import org.eclipse.jdt.core.dom.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

public class ElementEnumGetMappingsGenerator extends ClassMethodGenerator {
    private String enumClassName;
    private IGspCommonField field;

    public  ElementEnumGetMappingsGenerator(String enumClassName)
    {

        this.enumClassName = enumClassName;
    }
    public  ElementEnumGetMappingsGenerator(String enumClassName, IGspCommonField field)
    {
        this.enumClassName = enumClassName;
        this.field=field;
    }
    @Override
    protected Block buildMethodBody() {
        Block block=ast.newBlock();
        EnumIndexType enumIndexType=field.getEnumIndexType();
        InfixExpression expression=ast.newInfixExpression();
        IfStatement ifStatement=ast.newIfStatement();
        expression.setLeftOperand(ast.newSimpleName("mappings"));
        expression.setOperator(InfixExpression.Operator.EQUALS);
        expression.setRightOperand(ast.newNullLiteral());

        ifStatement.setExpression(expression);
        block.statements().add(ifStatement);

        //
        Assignment assignment1=ast.newAssignment();
        assignment1.setLeftHandSide(ast.newSimpleName("mappings"));
        assignment1.setOperator(Assignment.Operator.ASSIGN);
        //
        ClassInstanceCreation classInstanceCreation=ast
                .newClassInstanceCreation();
        //
//        QualifiedName qualifiedName2=ast.newQualifiedName(ast.newSimpleName("java"),ast.newSimpleName("util"));
//        Type type2=ast.newSimpleType(
//                ast.newQualifiedName(qualifiedName2,
//                        ast.newSimpleName("HashMap")));
        Type type2=ast.newSimpleType(ast.newSimpleName("HashMap"));
        ParameterizedType parameterizedType2=ast.newParameterizedType(type2);
        //parameterizedType2.typeArguments().add(ast.newSimpleType(ast.newSimpleName("Integer")));
        if(enumIndexType==EnumIndexType.String) {
            parameterizedType2.typeArguments().add(ast.newSimpleType(ast.newSimpleName("String")));
        }else{
            parameterizedType2.typeArguments().add(ast.newSimpleType(ast.newSimpleName("Integer")));
        }
        parameterizedType2.typeArguments().add(ast.newSimpleType(ast.newSimpleName(enumClassName)));
        classInstanceCreation.setType(parameterizedType2);

        assignment1.setRightHandSide(classInstanceCreation);
        ExpressionStatement expressionStatement=ast.newExpressionStatement(assignment1);

        Block block2=ast.newBlock();
        block2.statements().add(expressionStatement);

        ifStatement.setThenStatement(block2);

        //endregion

        Expression expression1=ast.newSimpleName("mappings");
        ReturnStatement returnStatement=ast.newReturnStatement();
        returnStatement.setExpression(expression1);
        block.statements().add(returnStatement);
        //

        return block;
    }

    @Override
    protected String getMethodName() {
        return "getMappings";
    }

    @Override
    protected TypeInfo getReturnType() {

        TypeInfo typeInfo=new TypeInfo(HashMap.class);

        EnumIndexType enumIndexType=field.getEnumIndexType();
        typeInfo.setIsParameterizedType(true);
        List<TypeInfo> arguments=new ArrayList<TypeInfo>();
        //判断枚举索引类型
        if(enumIndexType==EnumIndexType.String) {
            TypeInfo typeInfo1 = new TypeInfo("String");
            arguments.add(typeInfo1);
        }else {
            TypeInfo typeInfo1 = new TypeInfo("Integer");
            arguments.add(typeInfo1);
        }
        TypeInfo typeInf2=new TypeInfo(enumClassName);
        arguments.add(typeInf2);
        typeInfo.setArguments(arguments);
        return typeInfo;

    }

    @Override
    protected ArrayList<Modifier.ModifierKeyword> getAccessModifiers() {
        return new ArrayList<>(Arrays.asList(new Modifier.ModifierKeyword[]{Modifier.ModifierKeyword.PRIVATE_KEYWORD,Modifier.ModifierKeyword.STATIC_KEYWORD}));
    }

    @Override
    protected ArrayList<ParameterInfo> getParameterCollection() {
        return null;
    }


}
